﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Localization;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminReportList : UserControlBase
    {
        [AccessedThroughProperty("cmdAdd")]
        private LinkButton _cmdAdd;
        [AccessedThroughProperty("cmdSearch")]
        private LinkButton _cmdSearch;
        [AccessedThroughProperty("dgReports")]
        private DataGrid _dgReports;
        private bool _IsEditable;
        [AccessedThroughProperty("txtSearch")]
        private TextBox _txtSearch;
        private string ResourceFile;

        public event AddButtonEventHandler AddButton;

        public event EditCommandEventHandler EditCommand;

        public event ItemCommandEventHandler ItemCommand;

        public event ReturnButtonEventHandler ReturnButton;

        public event SearchButtonEventHandler SearchButton;

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            
            if (this.AddButton != null)
            {
                AddButton(RuntimeHelpers.GetObjectValue(sender), e);
            }
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            this.dgReports.CurrentPageIndex = 0;
            
            if (this.SearchButton != null)
            {
                SearchButton(RuntimeHelpers.GetObjectValue(sender), e);
            }
        }

        private void dgProducts_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgReports.CurrentPageIndex = e.NewPageIndex;
            this.populateList();
        }

        private void dgReports_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Copy")
            {
                DataGridItem item = e.Item;
                int reportID = int.Parse(e.CommandArgument.ToString());
                new SQLReportController().CopyReport(reportID);
            }
            
            if (this.ItemCommand != null)
            {
                ItemCommand(RuntimeHelpers.GetObjectValue(source), e);
            }
        }

        private void dgReports_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                NB_Store_SQLReportInfo dataItem = (NB_Store_SQLReportInfo) e.Item.DataItem;
                Control control = e.Item.Controls[0].Controls[0];
                if (control is ImageButton)
                {
                    ImageButton button = (ImageButton) control;
                    if (!this._IsEditable)
                    {
                        button.Visible = false;
                    }
                }
                control = e.Item.Controls[4].Controls[0];
                if (control is ImageButton)
                {
                    ImageButton button2 = (ImageButton) control;
                    button2.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdCopyReport", this.ResourceFile) + "');");
                    if (!this._IsEditable)
                    {
                        button2.Visible = false;
                    }
                }
                if (!dataItem.AllowExport)
                {
                    control = e.Item.Controls[6].Controls[0];
                    if (control is ImageButton)
                    {
                        ImageButton button3 = (ImageButton) control;
                        button3.Visible = false;
                    }
                }
                if (!dataItem.AllowDisplay)
                {
                    control = e.Item.Controls[5].Controls[0];
                    if (control is ImageButton)
                    {
                        ImageButton button4 = (ImageButton) control;
                        button4.Visible = false;
                    }
                }
                control = e.Item.Controls[7].Controls[0];
                if (control is ImageButton)
                {
                    ImageButton button5 = (ImageButton) control;
                    button5.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdEmailReport", this.ResourceFile) + "');");
                    if (!dataItem.EmailResults)
                    {
                        button5.Visible = false;
                    }
                }
            }
        }

        public void populateList()
        {
            SQLReportController controller = new SQLReportController();
            if (!this._IsEditable)
            {
                this.cmdAdd.Visible = false;
            }
            this.ResourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
            DataGrid dgReports = this.dgReports;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgReports, this.ResourceFile);
            this.dgReports = dgReports;
            ArrayList list = controller.GetSQLAdminReportList(this.PortalSettings.PortalId, this._IsEditable, this.txtSearch.Text);
            this.dgReports.DataSource = list;
            this.dgReports.DataBind();
        }

        protected virtual LinkButton cmdAdd
        {
            get
            {
                return this._cmdAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdAdd_Click);
                if (this._cmdAdd != null)
                {
                    this._cmdAdd.Click -= handler;
                }
                this._cmdAdd = value;
                if (this._cmdAdd != null)
                {
                    this._cmdAdd.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdSearch
        {
            get
            {
                return this._cmdSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdSearch_Click);
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click -= handler;
                }
                this._cmdSearch = value;
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click += handler;
                }
            }
        }

        protected virtual DataGrid dgReports
        {
            get
            {
                return this._dgReports;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridPageChangedEventHandler handler = new DataGridPageChangedEventHandler(this.dgProducts_PageIndexChanged);
                DataGridItemEventHandler handler2 = new DataGridItemEventHandler(this.dgReports_ItemDataBound);
                DataGridCommandEventHandler handler3 = new DataGridCommandEventHandler(this.dgReports_ItemCommand);
                if (this._dgReports != null)
                {
                    this._dgReports.PageIndexChanged -= handler;
                    this._dgReports.ItemDataBound -= handler2;
                    this._dgReports.ItemCommand -= handler3;
                }
                this._dgReports = value;
                if (this._dgReports != null)
                {
                    this._dgReports.PageIndexChanged += handler;
                    this._dgReports.ItemDataBound += handler2;
                    this._dgReports.ItemCommand += handler3;
                }
            }
        }

        public bool IsEdit
        {
            get
            {
                return this._IsEditable;
            }
            set
            {
                this._IsEditable = value;
            }
        }

        protected virtual TextBox txtSearch
        {
            get
            {
                return this._txtSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtSearch = value;
            }
        }

        public delegate void AddButtonEventHandler(object sender, EventArgs e);

        public delegate void EditCommandEventHandler(object source, DataGridCommandEventArgs e);

        public delegate void ItemCommandEventHandler(object source, DataGridCommandEventArgs e);

        public delegate void ReturnButtonEventHandler(object sender, EventArgs e);

        public delegate void SearchButtonEventHandler(object sender, EventArgs e);
    }
}

